/**
 * @(#) PassWordCheckProxy.as
 *
 * @date		2009. 02. 20.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	비밀번호 체크 - Proxy
 * 				
 */
package com.skt.common_hr.myprofile.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.myprofile.model.vo.PasswordVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class PassWordCheckProxy extends CommonProxy
	{
		public static const NAME:String = "PassWordCheckProxy";
		public static const LOAD_PASSWORD_COMPLETE:String			= "loadPasswordComplete";
		public static const LOAD_PASSWORD_FAILED:String				= "loadPasswordFailed";
		public static const SAVE_NEWPASSWORD_COMPLETE:String		= "saveNewpasswordComplete";
		public static const SAVE_NEWPASSWORD_FAILED:String			= "saveNewpasswordFailed";
		public static const CHECK_PASSWORD_COMPLETE:String			= "checkPasswordComplete";
		public static const CHECK_PASSWORD_FAILED:String			= "checkPasswordFailed";
		public static const CHANGE_PASSWORD_COMPLETE:String			= "changePasswordComplete";
		public static const CHANGE_PASSWORD_FAILED:String			= "changePasswordFailed";
		
		public static const VALID_REGNO_COMPLETE:String				= "VALID_REGNO_COMPLETE";
		public static const VALID_REGNO_FAILED:String				= "VALID_REGNO_FAILED";
		
		public var validateCode:int = 0;
		public var validateText:String = "";
		public var passwordInfo:PasswordVO;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PassWordCheckProxy(data:Object=null)
		{
			super(NAME, data);
		}
		


		public var passWord:String;


		/**
		 * 등록된 password정보를 가져온다.
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function loadPasswordInfo():void
		{
			var param:Object = new Object();
			param["I_PERNR"] = Variables.myPernr;		// 사번
			param["I_ACTION"] = "E";					// 처리구분('E'-존재여부확인,'N'-신규,'U'-변경,'C'-비번체크)
			
			loadData("Z_HR_0063", param, loadResult, loadFault);
		}
		

		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function newPassSave(newPass:String, reconfirmPass:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= Variables.myPernr;	// 사번
			param["I_NEWPASSWORD"] 	= newPass;				// 신규비밀번호
			param["I_VERIPASSWORD"] = reconfirmPass;		// 신규비밀번호 검증
			param["I_ACTION"] 		= "N";					// 처리구분
			
			loadData("Z_HR_0063", param, newPassSaveResult, newPassSaveFault);
		}
		
		public function checkPass(oldPass:String):void
		{	
			var param:Object = new Object();
			param["I_PERNR"] 		= Variables.myPernr;	// 사번
			param["I_PASSWORD"] 	= oldPass;				// 비밀번호
			param["I_ACTION"] 		= "C";					// 처리구분
			
			loadData("Z_HR_0063", param, checkPassResult, checkPassFault);
		}
		
		public function changePass(newPass:String, reconfirmPass:String, oldPass:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= Variables.myPernr;	// 사번
			param["I_PASSWORD"] 	= oldPass;				// 비밀번호
			param["I_NEWPASSWORD"] 	= newPass;				// 신규비밀번호
			param["I_VERIPASSWORD"] = reconfirmPass;		// 신규비밀번호 검증
			param["I_ACTION"] 		= "U";					// 처리구분
			
			loadData("Z_HR_0063", param, changePassResult, changePassFault);
		}
		
		public function validRegno(regno:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= Variables.myPernr;	// 사번
			param["I_ACTION"] 		= 'D';				
			param["I_REGNO"] 		= regno;			
			loadData("Z_HR_0063", param, vaildRegnoResult, changePassFault);
		}		

		public function saveInitPass(newPass:String, reconfirmPass:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] 		= Variables.myPernr;	// 사번
			param["I_NEWPASSWORD"] 	= newPass;				// 신규비밀번호
			param["I_VERIPASSWORD"] = reconfirmPass;		// 신규비밀번호 검증
			param["I_ACTION"] 		= "N";					// 처리구분
						
			loadData("Z_HR_0063", param, changePassResult, changePassFault);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				validateCode = event.result["VALCODE"];
				validateText = event.result["VALTEXT"];
				if(event.result.STATUSINFO.length>0) {
					passwordInfo = parsePasswordInfo(event.result.STATUSINFO);
				}
				facade.sendNotification(PassWordCheckProxy.LOAD_PASSWORD_COMPLETE, this);
			}
			else {
				facade.sendNotification(PassWordCheckProxy.LOAD_PASSWORD_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(PassWordCheckProxy.LOAD_PASSWORD_FAILED, this);
		}

		/**
		 * 신규 PASSWORD 저장 성공
		 */
		private function newPassSaveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				validateCode = event.result["VALCODE"];
				validateText = event.result["VALTEXT"];
				facade.sendNotification(PassWordCheckProxy.SAVE_NEWPASSWORD_COMPLETE, this);
			}
			else {
				facade.sendNotification(PassWordCheckProxy.SAVE_NEWPASSWORD_FAILED, this);
			}
		}
		
		/**
		 * * 신규 PASSWORD 저장 실패
		 */
		private function newPassSaveFault(event:FaultEvent):void
		{
			facade.sendNotification(PassWordCheckProxy.SAVE_NEWPASSWORD_FAILED, this);
		}
		
		/**
		 * 비밀번호 체크 성공
		 */
		private function checkPassResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				validateCode = event.result["VALCODE"];
				validateText = event.result["VALTEXT"];
				facade.sendNotification(PassWordCheckProxy.CHECK_PASSWORD_COMPLETE, this);
			}
			else {
				validateText = event.result["VALTEXT"];
				facade.sendNotification(PassWordCheckProxy.CHECK_PASSWORD_FAILED, this);
			}
		}
		
		/**
		 * 비밀번호초기화를 위한 주민등록번호 인증 성공
		 */
		private function vaildRegnoResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(PassWordCheckProxy.VALID_REGNO_COMPLETE, this);
			}
			else {
				Alert.show(event.result.RETTEXT);
				//facade.sendNotification(PassWordCheckProxy.VALID_REGNO_FAILED, this);
			}
		}
		
		/**
		 * 비밀번호 체크 실패
		 */
		private function checkPassFault(event:FaultEvent):void
		{
			facade.sendNotification(PassWordCheckProxy.CHECK_PASSWORD_FAILED, this);
		}
		
		/**
		 * 비밀번호변경 성공
		 */
		private function changePassResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				validateCode = event.result["VALCODE"];
				validateText = event.result["VALTEXT"];
				facade.sendNotification(PassWordCheckProxy.CHANGE_PASSWORD_COMPLETE, this);
			}
			else {
				facade.sendNotification(PassWordCheckProxy.CHANGE_PASSWORD_FAILED, this);
			}
		}
		
		/**
		 * 비밀번호 변경 실패
		 */
		private function changePassFault(event:FaultEvent):void
		{
			facade.sendNotification(PassWordCheckProxy.CHANGE_PASSWORD_FAILED, this);
		}
		
		private function parsePasswordInfo(src:ArrayCollection):PasswordVO
		{
			var pwInfo:PasswordVO = new PasswordVO;
			pwInfo.STATE 	= src[0].STATE;
			pwInfo.LCNT 	= src[0].LCNT;
			pwInfo.LDATE	= DateUtil.stringToDateTime(src[0].LDATE);
			if(pwInfo.LDATE==null) {
				pwInfo.LDATE = new Date();
			}
			var tmp:Date = DateUtil.stringToDateTime(src[0].LTIME);
			pwInfo.LDATE.hours			= tmp.hours;
			pwInfo.LDATE.minutes		= tmp.minutes;
			pwInfo.LDATE.seconds		= tmp.seconds;
			pwInfo.LDATE.milliseconds	= tmp.milliseconds;
			
			/*
			pwInfo.LDATE.hours 		= src[0].LTIME.hours;
			pwInfo.LDATE.minutes	= src[0].LTIME.minutes;
			pwInfo.LDATE.seconds	= src[0].LTIME.seconds;
			pwInfo.LDATE.milliseconds = src[0].LTIME.milliseconds;
			*/
			pwInfo.UPDPASS	= DateUtil.stringToDateTime(src[0].UPDPASS);
			
			return pwInfo;
		}
		
	}
}